using System;
using System.Collections.Generic;
using System.Text;

namespace Mud.Common.Messages
{

    /// <summary>
    /// Class which contains utility methods for messages.
    /// </summary>
    public static class MessageUtil
    {

        /// <summary>
        /// Packs the message.
        /// </summary>
        /// <param name="message">Message to pack.</param>
        public static BytePackage Pack(IMessage message)
        {
            if (message == null)
                throw new ArgumentNullException("message");

            BytePackage package = BytePackage.CreateNew();
            message.Serialize(package);
            return package;
        }

        /// <summary>
        /// Checks if the message is an instance of MessageResponseOK message.
        /// </summary>
        public static bool IsOK(IMessage message)
        {
            if (message == null)
                throw new ArgumentNullException("message");

            return message.MessageTypeId == (int)MessageTypeIds.ResponseOk;
        }

        /// <summary>
        /// Checks if the message is an instance of MessageResponseError message.
        /// </summary>
        public static bool IsError(IMessage message)
        {
            if (message == null)
                throw new ArgumentNullException("message");

            return message.MessageTypeId == (int)MessageTypeIds.ResponseError;
        }

        /// <summary>
        /// Gets the error message if the message is of MessageResponseError.
        /// </summary>
        public static string GetErrorMessage(IMessage message)
        {
            if (!IsError(message))
                throw new InvalidOperationException("The message is not an error message");

            return ((MessageResponseError)message).Description;
        }
    }
}
